package com.mk.cloud.pt.controller.platform;

import act.apidoc.Description;
import act.controller.Controller;
import act.inject.DefaultValue;
import com.mk.cloud.common.domain.PArticle;
import com.mk.cloud.common.domain.PArticle;
import com.mk.cloud.common.domain.PProduct;
import com.mk.cloud.common.enums.ErrorCode;
import com.mk.cloud.common.enums.GlobalValues;
import com.mk.cloud.common.exception.UserException;
import com.mk.cloud.common.interceptor.ITransacte;
import com.mk.cloud.common.interceptor.cookie.CookieService;
import com.mk.cloud.common.interceptor.cookie.ICLogin;
import com.mk.cloud.common.util.DateUtils;
import org.osgl.util.S;import com.mk.cloud.common.util.S2;
import org.beetl.sql.core.engine.PageQuery;
import org.osgl.mvc.annotation.*;

import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("article")
public class PArticleController extends CookieService {

    @Inject
    private PArticle.Mapper pArticleMapper;


    @Description("商品--商品列表")
    @With(ICLogin.class)
    @GetAction("pageList.json")
    public PageQuery<PArticle> pArticleList(
            @Description("筛选条件-文章标题") String arttitle,
            @Description("筛选条件-文章类型") String arttype,
            @Description("筛选条件-发布时间开始时间") String publishtimeb,
            @Description("筛选条件-发布时间结束时间") String publishtimee,
            @Description("当前第几页") @NotNull Integer currentPage,
            @Description("每页多少条") @DefaultValue("15") Integer pageSize

    ) {
        PageQuery<PArticle> query = new PageQuery<>();
        query.setPageSize(pageSize);
        query.setPageNumber(currentPage);
        Map<String, Object> map = new HashMap<>();
        if(S.isNotBlank(publishtimeb)){
            publishtimeb = publishtimeb+" 00:00:00";
        }
        if(S.isNotBlank(publishtimee)){
            publishtimee = publishtimee+" 23:59:59";
        }
        map.put("arttitle", arttitle);
        map.put("arttype", arttype);
        map.put("publishtimeb", publishtimeb);
        map.put("publishtimee", publishtimee);
        map.put("coid", me.getCoid());
        query.setParas(map);
        //此处的sql是三个sql
        pArticleMapper.getSQLManager().pageQuery("pArticle.select_article_page_list", PArticle.class, query);
        return query;
    }

    @Description("音频类文章")
    @With(ICLogin.class)
    @GetAction("artList.json")
    public List<PArticle> audioPArticleList(
    ) {
        PArticle article = new PArticle();
        article.setCoid(me.getCoid());
        article.setArttype(GlobalValues.ARTTYPE_5.value());
        return pArticleMapper.template(article);

    }


    @Description("文章--文章详情")
    @With(ICLogin.class)
    @GetAction("get.json")
    public PArticle getPArticle(@Description("文章id") String artid) {
        if (S.isEmpty(artid)) {
            return new PArticle();
        }
        return pArticleMapper.single(artid);
    }

    @Description("文章--文章保存")
    @With({ICLogin.class, ITransacte.class})
    @PostAction("save.json")
    public String savePArticle(@Description("文章对象") PArticle article) {
        article.setArtcontent(S2.rightDoArticle(article.getArtcontent()));
        article.setCoid(me.getCoid());
        if(S.eq(article.getArtvalistate(), GlobalValues.ARTSTATE_1.value())){
            article.setPublishtime(DateUtils.getTimeNow());
            article.setUserid(me.getUserid());
        }
        if (S.isNotBlank(article.getArtid())) {
            pArticleMapper.updateTemplateById(article);
            return article.getArtid();
        } else {
            String artid = S2.UUID();
            article.setArtid(artid);
            pArticleMapper.insert(article);
            return artid;
        }
    }

    @Description("文章--文章状态修改")
    @With({ICLogin.class, ITransacte.class})
    @PutAction("update.json")
    public String updatePArticle(@Description("文章id") String artid,@Description("文章状态") String artvalistate) {
        PArticle article = new PArticle();
        article.setArtid(artid);
        article.setArtvalistate(artvalistate);
        if(S.eq(artvalistate, GlobalValues.ARTSTATE_1.value())){
            article.setPublishtime(DateUtils.getTimeNow());
            article.setUserid(me.getUserid());
        }
        pArticleMapper.updateTemplateById(article);
        return article.getArtid();
    }

    @Description("文章--文章删除")
    @With({ICLogin.class, ITransacte.class})
    @DeleteAction("{artid}/del.json")
    public Integer delPArticle(@Description("文章id") String artid) {
        return pArticleMapper.deleteById(artid);
    }


}
